﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Accessibility;

namespace webSurfer
{
    public class Support
    {
        public static mshtml.IHTMLElement GetElement(int hwnd, string id)
        {
            //object wref = new object[] { this };
            SHDocVw.ShellWindowsClass shw = new SHDocVw.ShellWindowsClass();
            //Cursor bakCursor = this.Cursor;
            //this.Cursor = Cursors.WaitCursor;
            int resu = 0;
            //shw.FindWindowSW(ref wref, ref wref, 1, out  resu, 5);
            SHDocVw.IWebBrowser ie = null;
            SHDocVw.IWebBrowser2 ie2;
            foreach (object webb in shw)
            {
                ie = (SHDocVw.IWebBrowser)webb;
                ie2 = (SHDocVw.IWebBrowser2)webb;
                try
                {
                    if (ie2.HWND == hwnd)
                        break;
                }
                catch { }
            }
            mshtml.IHTMLElement ele = null;
            if (ie != null)
            {
                mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)ie.Document;
                mshtml.IHTMLElementCollection bts = doc.getElementsByTagName("Input");
                foreach (mshtml.IHTMLElement elelook in bts)
                {
                    if (elelook.id == id)
                    {
                        ele = elelook;
                        break;
                    }
                    string name = (string)elelook.getAttribute("name", 0);
                    if (name == id)
                    {
                        ele = elelook;
                        break;
                    }
                    if (elelook.innerHTML == id)
                    {
                        ele = elelook;
                        break;
                    }

                }
            }
            return ele;
        }
    }
}
